var ExternalInterface = function () {

    var basePath = ROOT_PATH + 'apiManager/externalInterface/',
        pagePath = ROOT_PATH + 'pages/externalInterface/',
        $dataGrid = null,
        noSelectedMsg = '请选择';


    /**
     * 初始化数据表格
     * @param gridId
     */
    function initGrid(gridId) {
        $dataGrid = BsGridUtil.initDataGrid({
            gridId: gridId,
            url: basePath + 'paging',
            singleSelect: false,
            columns: [[
                {field: 'id', title: 'id', hidden: false, checkbox: true},
                {field: 'address', title: '接口访问地址', hidden: false, sortable: true, width: 100},
                {field: 'remarks', title: '接口描述', hidden: false, sortable: true, width: 100},
                {
                    field: 'enable', title: '是否启用', hidden: false, sortable: true, width: 100,
                    formatter: function (val, row) {
                        return val ? "是" : "否";
                    }
                },
                {field: 'name', title: '接口名称', hidden: false, sortable: true, width: 100},
                {
                    field: 'action', title: '操作',
                    formatter: function (value, row, index) {
                        var html = '<a href="javascript:ExternalInterface.edit(' + row.id + ')"><div class="btn-sml icon-edit" title="修改"></div></a>';
                        if (row.enable) {
                            html += '<a href="javascript:ExternalInterface.disable(' + row.id + ')"><div class="btn-sml icon-edit" title="禁用"></div></a>';
                        } else {
                            html += '<a href="javascript:ExternalInterface.enable(' + row.id + ')"><div class="btn-sml icon-edit" title="启用"></div></a>';
                        }
                        return html;
                    }
                }
            ]]
        });
    }


    /**
     * 获取当前已选择的external_interface
     * @returns {*|{js, css, dependencies}}
     */
    function getSelectedExternalInterface() {
        return $dataGrid.datagrid('getSelected');
    }

    /**
     * 编辑
     */
    function edit(id) {
        if (id) {
            $.Get(basePath + 'get', {id: id}, function (data) {
                var dlg = FormUtils.openDialog({
                    domId: 'editDl', //打开dialog的容器
                    href: pagePath + 'externalInterface-edit.html',
                    url: basePath + 'edit',
                    success: function (data) {
                        reloadDataGrid();
                    }, //成功时候执行的方法
                    onLoad: function () {
                        FormUtils.setValues($(dlg).find('form:first'), data.data);
                    },
                    title: '修改'
                });
            });
        } else {
            Dialog.warning(noSelectedMsg);
        }
    }

    /**
     * 刷新表格
     */
    function reloadDataGrid() {
        $dataGrid.datagrid('reload');
    }


    /**
     * 创建
     */
    function create() {
        FormUtils.openDialog({
            domId: 'createDl', //打开dialog的容器
            href: pagePath + 'externalInterface-create.html',
            url: basePath + 'create',
            success: function (data) {
                reloadDataGrid();
            }, //成功时候执行的方法
            title: '创建'
        });
    }

    /**
     * 禁用
     */
    function disable(id) {
        $.Ajax({
            url: basePath + "disable",
            data: {
                id: id
            },
            success: function () {
                Dialog.success("禁用成功");
            }
        });
    }

    /**
     * 启用
     */
    function enable(id) {
        $.Ajax({
            url: basePath + "enable",
            data: {
                id: id
            },
            success: function () {
                Dialog.success("启用成功");
            }
        });
    }


    return {
        initGrid: initGrid,
        create: create,
        edit: edit,
        disable: disable,
        enable: enable
    }
}();